import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Link } from 'react-router'
import { Button, Spin, Form, Input, Table, DatePicker, Select,Modal,message } from 'antd'
import moment from 'moment';
import ModalUpgradeMaster from './upgradeModal.js';
import ShowBigImg from '../../components/showBigImg'
/* 引用接口文档路径 */
import {fetchHouseCheckList,attestation,showUserInfo} from 'actions/userConter'

import {getbasetypefetch} from 'actions/common'

import '../../style/userControl.less'

const { MonthPicker, RangePicker } = DatePicker;
const FormItem = Form.Item
@Form.create({})
//abc
@connect(
  (state, props) => ({
    config: state.config,
    userInfoList: state.userInfoList,
  })
)
export default class app extends Component {
  constructor(props) {
    super(props)
    this.state = {
      list:"",//基础数据列表
      ModalFlg:false,//弹窗状态
      upgradeModalFlg:false,//升级为大咖弹窗
      upgradeModalId:"",//升级大咖的用户ID
      userList:"",
      auditStatus:[],//认证状态
      showBigImgModal:false,//查看大图的弹窗
      imgArry:[],//图片数组
      imgIndex:"",//图片下标
    }
    this.keyword={"userName":"","mobile":"","authStatus":"","userType":"2"};//搜索关键词
    this.currentNumber = 1;//当前页面
    this.pagesize = 10;//每页条数
  }
  componentDidMount() {
    this.selectList();
    this.getAuditStatus();
  }
  
  selectFn(){//点击搜索按钮
    this.keyword = {
      "userName":this.props.form.getFieldValue('userName'),
      "mobile":this.props.form.getFieldValue('mobile'),
      "authStatus":this.props.form.getFieldValue('authStatus'),
      "userType":"2"
    };
    this.currentNumber=1;
    this.selectList();
  }
  selectList(pageNumber){//查询列表
    const datajson = Object.assign({"start":pageNumber||1,"end":this.pagesize}, this.keyword);
    this.currentNumber = pageNumber||1;
    this.props.dispatch(fetchHouseCheckList(
      datajson,
      (respose) => {
        this.setState({
          list: respose,
        })
    }))
  }
  
  getAuditStatus(){
    this.props.dispatch(getbasetypefetch(
      {"type":7},
      (respose) => {
        this.setState({
          auditStatus: respose.respData.resultData,
        })
    }))
  }


  selectInfo(id){//查询用户详细信息
    this.setState({
      ModalFlg: true,
    })
    //根据ID查询单条数据
    this.props.dispatch(showUserInfo({"accountId":id,},(respose) => {
      this.setState({
        userList: respose,
      })
    }))
  }

  ModalSubmit(id,status){//弹窗操作 status 1通过 2拒绝
    Modal.confirm({
      content:"确定要执行操作吗？",
      onOk:()=>{
        this.props.dispatch(attestation({"accountId":id,"status":status},(respose) => {
          this.ModalCancel();
          this.selectList(this.currentNumber);
          message.success(respose.respMsg)
        }))
      },
      onCancel:()=>{
      }
    })
    
  }

  showBigImg(item,index){//点击查看大图
    //item 图片数组 index 点击的下标
    //单张图片
    var arry = [item];
    this.setState({
      showBigImgModal:true,
      imgArry:arry,
      imgIndex:index
    })
  }
  showBigImgModalCancel(){//关闭查看大图弹窗
    this.setState({
      showBigImgModal:false,
      imgArry:[],
      imgIndex:''
    })
  }

  uploadBtn(id){//打开升级大咖弹窗
    //关闭弹窗
    this.setState({
      upgradeModalFlg: true,
      upgradeModalId:id,
    },()=>{
      this.setState({
        userList: "",
      })
    })
  }

  ModalCancel(){//弹窗关闭
    //关闭弹窗
    this.setState({
      ModalFlg: false,
    },()=>{
      this.setState({
        userList: "",
      })
    })
  }

  upgradeModalCancel(){//升级大咖弹窗关闭
    //关闭弹窗
    this.setState({
      upgradeModalFlg: false,
    },()=>{
      this.setState({
        userList: "",
      })
    })
  }


  columns() {//表格标题
    return [
      {
        title: '用户编码',
        key: 'userCode',
        dataIndex: 'userCode',        
        width: '10%',
      },
      {
        title: '头像',
        dataIndex: 'headImage',
        key: 'headImage',
        width: '10%',
        render: (text, recordId, index) => <img 
          style={{width:"50px",cursor:"pointer"}} 
          title="点击查看大图" 
          onClick={this.showBigImg.bind(this,text ? text :  require('../../images/baseHeader.png'),0)} 
          src={text ? text : require('../../images/baseHeader.png')} 
        />,
      },
      {
        title: '姓名',
        dataIndex: 'userName',
        key: 'userName',
        width: '10%',
      },
      {
        title: '手机号',
        dataIndex: 'mobile',
        key: 'mobile',
        width: '10%',
      },
      {
        title: '公司职位',
        dataIndex: 'positionName',
        key: 'positionName',
        width: '10%',
      },
      {
        title: '注册时间',
        dataIndex: 'createTime',
        key: 'createTime',
        width: '15%',
      },
      {
        title: '认证状态',
        dataIndex: 'isAuth',
        key: 'isAuth',
        width: '10%',
        render: (text, recordId, index) => 
        <span>
          {text == 2 ? '已通过':(text == 1 ? '待认证' : '认证失败')}
        </span>,
      },
      {
        title: '认证图片',
        key: 'certificateImage',
        dataIndex: 'certificateImage',        
        width: '10%',
        render: (text, recordId, index) => <img 
          style={{width:"50px",cursor:"pointer"}} 
          title="点击查看大图" 
          onClick={this.showBigImg.bind(this,text ? text + global.$GLOBALCONFIG.imgRotate :  require('../../images/baseImg.png'),0)} 
          src={text ? text + global.$GLOBALCONFIG.imgRotate : require('../../images/baseImg.png')} 
        />,
      },
      {
        title: '操作',
        // key: 'operate',
        // fixed: 'right',
        width: '10%',
        render: (text, recordId, index) => 
        <span>
          <Button type="primary" size="small" onClick={this.selectInfo.bind(this,text.id)} style={{marginRight:"5px",marginBottom:"5px"}}>查看</Button>
          {
            text.isAuth == 2 ? 
            <Button type="primary" size="small" onClick={this.uploadBtn.bind(this,text.accountId)} style={{marginRight:"5px",marginBottom:"5px"}}>升级</Button>
            :''
          }         
        </span>,
      },
    ]
  }
  


  render () {
    const {form } = this.props
    const { getFieldDecorator } = form    
    const accountList = this.state.list.respData ? this.state.list.respData : [];
    const joinList = this.state.userList.respData ? this.state.userList.respData :[];
    const account = this.state.userList.respData ? this.state.userList.respData.account :[];
    const dateFormat = 'YYYY/MM/DD';
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
    };
    return (
      <div className="page">
        {/* 搜索条件筛选块 */}
        <div className="searcDiv">
          <Form onSubmit={this.selectFn.bind(this)} layout="inline">
            <FormItem label="姓名">      
              {
                getFieldDecorator('userName', {
                  rules: [{
                    required: false,
                  }],
                })(
                  <Input placeholder="请输入关键字" size="default" style={{ width: '200px' }} />
                )
              }             
            </FormItem>
            <FormItem label="手机号">
              {
                getFieldDecorator('mobile', {
                  rules: [{
                    required: false,
                  }],
                })(
                  <Input placeholder="请输入关键字" size="default" style={{ width: '200px' }} />
                )
              }             
            </FormItem>
            <FormItem label="认证状态">
              {
                getFieldDecorator('authStatus', {
                  rules: [{
                    required: false,
                  }],
                })(
                  <Select placeholder="请选择认证状态"    style={{ width: 200 }} >
                    <Select.Option value={""} key={99}>全部</Select.Option>                  
                    {
                      this.state.auditStatus.map((item,i)=> {
                        return(
                          <Select.Option value={item.code} key={i}>{item.value}</Select.Option>
                        )
                      })
                    }
                  </Select>
                )
              }
            </FormItem>
            <Button type="primary" onClick={this.selectFn.bind(this)}>搜索</Button>
          </Form>
        </div>
        {/*数据渲染列表*/}
        <Spin spinning={false}>
          <Table
            rowKey="id" 
            columns={this.columns()}//表格标题列
            dataSource={accountList.accountList}
            pagination={{showTotal:total => `共 ${total} 条数据`, total: accountList.count, current:this.currentNumber,pageSize: this.pagesize, onChange: this.selectList.bind(this)}}
            scroll={{ y: true }}       
            rowClassName={(record, index) => record.isAuth == 1 ? 'tableRed' : ''}        
          />
        </Spin>
        {/*  详细信息展示弹窗 */}
        <Modal
          width={"80%"}
          visible={this.state.ModalFlg}
          title="认证操作"
          onCancel={this.ModalCancel.bind(this)}
          footer={
            account.isAuth == 1?
            [<Button key="submit" type="primary"  onClick={this.ModalSubmit.bind(this,account.id,2)}>通过</Button>,
            <Button key="cancel" type="primary"  onClick={this.ModalSubmit.bind(this,account.id,3)}>拒绝</Button>]
            :
            (account.isAuth == 2?
              // [<Button key="cancel" type="primary"  onClick={this.ModalSubmit.bind(this,account.id,3)}>拒绝</Button>]  
              ""            
              :
              [<Button key="submit" type="primary"  onClick={this.ModalSubmit.bind(this,account.id,2)}>通过</Button>]              
            )
        }
        >
          <ul className="joinBlackListModal">
            <li>
              <span className="title">用户编号:</span><span className="content">{account.id}</span>
            </li>
            <li>
              <span className="title">姓名:</span><span className="content">{account.userName}</span>
            </li>
            <li>
              <span className="title">公司:</span><span className="content">{account.companyName}</span>
            </li>
            <li>
              <span className="title">职位:</span><span className="content">{account.positionName}</span>
            </li>
            <li>
              <span className="title">头像:</span>
              <span className="content">
                <img 
                  style={{width:"80px",cursor:"pointer"}} 
                  title="点击查看大图" 
                  onClick={this.showBigImg.bind(this,account.headImage ? account.headImage :  require('../../images/baseHeader.png'),0)} 
                  src={account.headImage ?account.headImage : require('../../images/baseHeader.png')} 
                />
              </span>
            </li>
            <li>
              <span className="title">认证图片:</span>
              <span className="content">
                <img 
                  style={{width:"80px",cursor:"pointer"}} 
                  title="点击查看大图" 
                  onClick={this.showBigImg.bind(this,account.certificateImage ? account.certificateImage+global.$GLOBALCONFIG.imgRotate :  require('../../images/baseImg.png'),0)} 
                  src={account.certificateImage ? account.certificateImage+global.$GLOBALCONFIG.imgRotate :  require('../../images/baseImg.png')} 
                />
              </span>
            </li>
            <li>
              <span className="title">认证状态:</span><span className="content">{account.isAuth == 2 ? '已通过':(account.isAuth == 1 ? '待认证' : '认证失败')}</span>
            </li>
            <li>
              <span className="title">注册时间:</span><span className="content">{account.createTime}</span>
            </li>
            <li>
              <span className="title">手机号码:</span><span className="content">{account.mobile}</span>
            </li>
            <li>
              <span className="title">帖子数量:</span><span className="content">{joinList.dynamicCount}</span>
            </li>
            <li>
              <span className="title">报告数量:</span><span className="content">{joinList.numberOfReports}</span>
            </li>
            <li>
              <span className="title">需求数量:</span><span className="content">{joinList.demand}</span>
            </li>
            <li>
              <span className="title">最后登录时间:</span><span className="content">{joinList.lastLoginTime}</span>
            </li>
          </ul>
        </Modal>
        {/* 升级大咖弹窗 */}
        {
          this.state.upgradeModalFlg?
          <ModalUpgradeMaster
            modalType="1"
            ModalFlg={this.state.upgradeModalFlg} 
            id={this.state.upgradeModalId}
            ModalCancel={this.upgradeModalCancel.bind(this)} 
            callback={this.selectList.bind(this)} 
          />
          :""
        }
        {/* 查看大图的弹窗 */}
        {
          this.state.showBigImgModal?
          <ShowBigImg visible={this.state.showBigImgModal} imgArry={this.state.imgArry} imgIndex={this.state.imgIndex} closeModal={this.showBigImgModalCancel.bind(this)} />
          :""
        }
        
      </div>
    )
  }
}